#include "TimeInfo.h"
#include <sstream>

std::string TimeInfo::GetTimeNow(const char* fmt /*= "%Y-%m-%d %H:%M:%S"*/, int time_zone /*= 8*/)
{
	std::stringstream mss_;
	mtm_.tm_sec = munix_sec_ % mkMinutesInHour_;
	int i = (munix_sec_ / mkMinutesInHour_);
	mtm_.tm_min = i % mkMinutesInHour_; //nn
	i /= mkMinutesInHour_;
	mtm_.tm_hour = (i + time_zone) % mkHoursInDay_; // hh
	mtm_.tm_mday = (i + time_zone) / mkHoursInDay_;
	int a = mtm_.tm_mday + mkDaysFromUnixTime_;
	int b = (a * 4 + 3) / mkMagicUnkonwnFirst_;
	int c = (-b * mkMagicUnkonwnFirst_) / 4 + a;
	int d = ((c * 4 + 3) / mkMagicUnkonwnSec_);
	int e = -d * mkMagicUnkonwnSec_;
	e = e / 4 + c;
	int m = (5 * e + 2) / mkDaysFromYear_;
	mtm_.tm_mday = -(mkDaysFromYear_ * m + 2) / 5 + e + 1;
	mtm_.tm_mon = (-m / 10) * 12 + m + 2;
	mtm_.tm_year = b * 100 + d - 6700 + (m / 10);

	mss_ << std::put_time(&mtm_, fmt); //#include <iomanip>
	return mss_.str();
}
